/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.workflow.view;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jcr.RepositoryException;
import javax.jcr.UnsupportedRepositoryOperationException;
import offset.nodes.Constants;
import offset.nodes.server.view.FacesUtils;
import offset.nodes.server.workflow.controller.WorkflowAttribute;
import offset.nodes.server.workflow.model.State;
import offset.nodes.server.workflow.model.WorkflowDocument;

/**
 *
 * @author Walter Lütgenau
 */
public class WorkflowBean {

    List<State> transitions = null;
    WorkflowDocument workflowDocument = null;

    protected void init() {
        WorkflowAttribute att = (WorkflowAttribute) FacesUtils.getRequest().getAttribute(WorkflowAttribute.ATT_WORKFLOW);
        if (att == null) {
            att = (WorkflowAttribute) FacesUtils.getRequest().getSession().getAttribute(WorkflowAttribute.ATT_WORKFLOW);
            if ( att == null)
                return;
            FacesUtils.getRequest().getSession().removeAttribute(WorkflowAttribute.ATT_WORKFLOW);
        }

        this.workflowDocument = att.getWorkflowDocument();
        if (att.getStates() != null)
            this.transitions = att.getStates();
    }

    public List<State> getTransitions() {
        init();
        return transitions;
    }

    public boolean isWorkflow() {
        init();
        return workflowDocument != null && workflowDocument.getNode() != null;
    }

    public boolean isNoWorkflow() {
        return !isWorkflow();
    }

    public String getWorkflowDocumentUUID() {
        init();
        try {
            if (workflowDocument.getNode() == null)
                return "";
            return workflowDocument.getNode().getUUID();
        } catch (RepositoryException ex) {
            Logger.getLogger(WorkflowBean.class.getName()).log(Level.SEVERE, null, ex);
            return "";
        }
    }

    public String getWorkflowDocumentPath() {
        init();
        try {
            if (workflowDocument.getNode() == null)
                return "";
            return workflowDocument.getNode().getPath();
        } catch (RepositoryException ex) {
            Logger.getLogger(WorkflowBean.class.getName()).log(Level.SEVERE, null, ex);
            return "";
        }
    }

    public String getWorkflowType() {
        return Constants.PAR_CONTENT_TYPE + "=" + Constants.CONTENT_TYPE_WORKFLOW;
    }
}
